package com.le.leauth.controller;

import com.le.leauth.domain.dto.LoginDTO;
import com.le.leauth.domain.dto.RegisterDTO;
import com.le.leauth.domain.vo.LoginUserInformationVO;
import com.le.leauth.service.AuthService;

import com.le.lecommon.common.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.security.auth.message.AuthException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.ServerException;

@CrossOrigin(origins = "http://localhost:3000") // 方式一:通过在Controller层注解的方式！！
@RestController
@Tag(name = "认证管理")
@RequiredArgsConstructor
@Slf4j
public class AuthController {

    private final AuthService authService;
    /**
     * 获取第三方授权地址
     * @return
     */
    @ApiOperation(value = "获取第三方授权地址")
    @GetMapping("/api/auth/render/{source}")
    public Result<String> getLoginAddress(@PathVariable String source) throws AuthException {
        log.info("开始进行第三方登录:{}",source);
        String re = authService.getAddress(source);
        System.out.println("授权地址:"+re);
        return Result.success(re);
    }

    /**
     * 用户第三方登录
     * AuthCallback callback：接收第三方登录回调的参数（如授权码、state 等相关数据）的封装对象。
     * @PathVariable String source：从 URL 路径中提取 {source} 的值（如 github）。
     * HttpServletResponse httpServletResponse：用于处理 HTTP 响应（如重定向）。
     * @param callback
     * @param httpServletResponse
     * @param source
     */
    @GetMapping("/api/auth/callback/{source}")
    @ApiOperation(value = "用户第三方登录")
    public void authLogin(AuthCallback callback, HttpServletResponse httpServletResponse, @PathVariable String source) throws AuthException, IOException {
        log.info("第三方登录并获取第三方登录的信息");
        authService.authLogin(callback,httpServletResponse,source);
    }

    /**
     * 登录接口
     * @param loginDTO
     * @return
     * @throws ServerException
     */
    @Operation( summary = "用户登录",description = "根据密码登录并返回携带token的数据")
    @PostMapping("/auth/login")
    public Result<LoginUserInformationVO> login(@RequestBody LoginDTO loginDTO) throws ServerException {
        log.info("用户开始登录, loginDTO={}", loginDTO);
        return Result.success(authService.loginAndGetInfo(loginDTO));
    }

    /**
     * 获取验证码
     * @param email
     * @return
     * @throws MessagingException
     */
    @Operation(summary = "获取验证码",description = "根据邮箱发送验证码给用户")
    @GetMapping("/api/sendEmailCode")
    public Result<Boolean> sendEmailCode(@RequestParam("email") String email) throws MessagingException {
        log.info("获取验证码, email={}", email);
        return Result.success(authService.sendEmail(email));
    }

    /**
     * 注册
     * @param registerDTO
     * @return
     */
    @Operation( summary = "注册", description = "根据验证码等注册")
    @PostMapping("/api/email/register")
    public Result<Boolean> register (@RequestBody RegisterDTO registerDTO){
        log.info("开始注册, registerDTO={}", registerDTO);
        return Result.success(authService.register(registerDTO));
    }

    /**
     *
     * @param registerDTO
     * @return
     */
    @Operation(summary = "修改密码", description = "根据用户和邮箱修改密码")
    @PostMapping("/api/email/forgot")
    public Result<Boolean> forgotPassword(@RequestBody RegisterDTO registerDTO){
        log.info("修改密码, registerDTO={}", registerDTO);
        return Result.success(authService.forgotPassword(registerDTO));

    }
}
